package livesun.justiceeternal.view.titlebar;
import android.os.Build;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import livesun.justiceeternal.utils.UiUtils;

/**
 * 类描述：4.4以上沉浸式工具类
 * 创建人：livesun
 * 创建时间：2017/8/29
 * 修改人：
 * 修改时间：
 * github：https://github.com/livesun
 */

public class TranslucentUtils {

    /**
     * 判断底部导航条展示
     * @return
     */
    public static boolean isVirtualKeyShow() {
        int resourceId = UiUtils.getResources().getIdentifier("config_showNavigationBar", "bool", "android");
        return resourceId > 0?UiUtils.getResources().getBoolean(resourceId):false;
    }

    /**
     * 获取状态栏高度
     * @return
     */
    public static int getStatusHeight() {
        int stastusHeight = -1;
        int statusId = UiUtils.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if(statusId > 0) {
            stastusHeight = UiUtils.getResources().getDimensionPixelSize(statusId);
        }
        return stastusHeight == -1?dp2px(24):stastusHeight;
    }

    /**
     * 获取底部导航条的高度
     * @return
     */

    private static int getNavigationHeight() {
        int navigationHeight = -1;
        int navigationId = UiUtils.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        if(navigationId > 0) {
            navigationHeight = UiUtils.getResources().getDimensionPixelSize(navigationId);
        }

        return navigationHeight == -1?dp2px(48):navigationHeight;
    }

    /**
     * 设置状态栏图标为深色和魅族特定的文字风格，Flyme4.0以上
     * 可以用来判断是否为Flyme用户
     * @param window 需要设置的窗口
     * @param dark 是否把状态栏字体及图标颜色设置为深色
     * @return  boolean 成功执行返回true
     *
     */
    public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            try {
                WindowManager.LayoutParams lp = window.getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                window.setAttributes(lp);
                result = true;
            } catch (Exception e) {

            }
        }
        return result;
    }

    /**
     * 需要MIUIV6以上
     * @param window
     * @param dark 是否把状态栏字体及图标颜色设置为深色
     * @return  boolean 成功执行返回true
     *
     */
    public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) {
        boolean result = false;

        if (window != null) {
            Class clazz = window.getClass();
            try {
                int darkModeFlag = 0;
                Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                if(dark){
                    extraFlagField.invoke(window,darkModeFlag,darkModeFlag);//状态栏透明且黑色字体
                }else{
                    extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
                }
                result=true;

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    //开发版 7.7.13 及以后版本采用了系统API，旧方法无效但不会报错，所以两个方式都要加上
                    if(dark){
                        window.getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                    }else {
                        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                    }
                }
            }catch (Exception e){

            }
        }
        return result;
    }


    private static int dp2px(int dp) {
        return (int)TypedValue.applyDimension(1, (float)dp, UiUtils.getResources().getDisplayMetrics());
    }
}
